public class BinarySearchTest {
    public static void main(String[] args) {
        int[] arr = {1, 5, 9, 23, 43, 58, 96}; //数组必须是有序的

      /*  int target = (int)(Math.random()*100);
        System.out.println("target = " + target);*/

        int target = 58;

        int left = 0;
        int right = arr.length - 1;

        int index = -1;
        while(left<=right){
            int mid = (left + right) / 2;
            if (target == arr[mid]) {
                //找到了
                index = mid;
                break;
            } else if (target > arr[mid]) {
                left = mid + 1;
            } else {  //target < arr[mid]
                right = mid - 1;
            }
        }

        if(index != -1){
            System.out.println("找到了，下标是" + index);
        }else{
            System.out.println("没找到");
        }
    }
}
